class Solution {
public:
    vector<int> processQueries(int c, vector<vector<int>>& connections, vector<vector<int>>& queries) {
        vector<int> table(c + 1, -1);
        for (auto& conn : connections) {
            int a = conn[0], b = conn[1];
            while (table[a] > 0) a = table[a];
            while (table[b] > 0) b = table[b];
            if (a == b) continue;
            if (a < b) table[a] += table[b], table[b] = a;
            else table[b] += table[a], table[a] = b;
        }
        for (int i = 1; i <= c; i++) {
            int src = i;
            while (table[src] > 0) src = table[src];
            int tmpi = i;
            while (table[tmpi] > 0) {
                int t = table[tmpi];
                table[tmpi] = src;
                tmpi = t;
            }
        }
        unordered_map<int, list<int>> src_map;
        for (int i = 1; i <= c; i++) {
            int a = i;
            if (table[a] > 0) a = table[a];
            src_map[a].push_back(i);
        }
        //for(int i = 1; i <= c; i++) cout << table[i] << ' ';

        vector<int> res;
        unordered_set<int> hash;
        for (auto& que : queries) {
            if (que[0] == 2) hash.insert(que[1]);
            else {
                if (hash.find(que[1]) == hash.end()) {
                    res.push_back(que[1]);
                    continue;
                }
                int a = que[1];
                if (table[que[1]] > 0) a = table[que[1]];
                list<int>& r = src_map[a];
                while (!r.empty() && hash.find(*r.begin()) != hash.end()) r.pop_front();
                if (r.empty()) res.push_back(-1);
                else res.push_back(*r.begin());
            }
        }

        return res;
    }
};